package com.tjf.lib_utils.SelectPricture;

import android.content.Context;

import com.luck.picture.lib.animators.AnimationType;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.style.PictureSelectorStyle;
import com.tjf.lib_utils.GsonUtils;
import com.tjf.lib_utils.LogsUtils;

import java.util.ArrayList;

/**
 * 选择图片工具类
 * <p>
 * //图片选择器 https://github.com/LuckSiege/PictureSelector
 * // PictureSelector 基础 (必须)
 * implementation 'io.github.lucksiege:pictureselector:v3.11.2'
 * <p>
 * // 图片压缩 (按需引入)
 * implementation 'io.github.lucksiege:compress:v3.11.2'
 * <p>
 * <!--Android 11 使用相机，需要再AndroidManifest.xm 添加如下代码：-->
 * <queries package="${applicationId}">
 * <intent>
 * <action android:name="android.media.action.IMAGE_CAPTURE">
 *
 * </action>
 * </intent>
 * <intent>
 * <action android:name="android.media.action.ACTION_VIDEO_CAPTURE">
 *
 * </action>
 * </intent>
 * </queries>
 */
public class PictureSelectorUtils {


    private PictureSelectorUtils() {
    }


    // 静态内部类持有外部类的唯一实例
    private static class SingletonHolder {
        private static final PictureSelectorUtils INSTANCE = new PictureSelectorUtils();
    }

    // 公共的静态方法，返回单例实例
    public static PictureSelectorUtils getInstance() {
        return SingletonHolder.INSTANCE;
    }


    public void openCamera(Context activity, onPictureSelectorListener selectorListener) {
        PictureSelector.create(activity).openCamera(SelectMimeType.ofImage())
                .setCompressEngine(new ImageCompressEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        LogsUtils.i("路经==" + GsonUtils.toJson(result));
                        ArrayList<String> filePathList = new ArrayList<>();
                        for (LocalMedia localMedia : result) {
                            filePathList.add(localMedia.getCompressPath());
                        }
                        selectorListener.onResult(filePathList);
                    }

                    @Override
                    public void onCancel() {
                        selectorListener.onCancel();
                    }
                });

    }
    public void showPictureSelectorImageOne(Context activity, onPictureSelectorListener selectorListener) {
        showPictureSelectorImage(activity, 1, true,
                false, selectorListener);
    }
    public void showPictureSelectorImageThree(Context activity, onPictureSelectorListener selectorListener) {
        showPictureSelectorImage(activity, 3, true,
                false, selectorListener);
    }
    public void showPictureSelectorImageFive(Context activity, onPictureSelectorListener selectorListener) {
        showPictureSelectorImage(activity, 5, true,
                false, selectorListener);
    }
    /**
     * 选择图片
     *
     * @param activity
     * @param maxSelectNum
     * @param isCamera
     * @param isGif
     * @param selectorListener
     */
    public void showPictureSelectorImage(Context activity, int maxSelectNum, boolean isCamera,
                                         boolean isGif, onPictureSelectorListener selectorListener) {
        PictureSelector.create(activity).openGallery(SelectMimeType.ofImage())
                .isWebp(false).setSelectorUIStyle(new PictureSelectorStyle())
                .setRecyclerAnimationMode(AnimationType.ALPHA_IN_ANIMATION)//相册列表动画效果
                .setMaxSelectNum(maxSelectNum)//最大选择数量,默认9张
                .isMaxSelectEnabledMask(true)//达到最大选择数是否开启禁选蒙层
                .setImageEngine(MyGlideEngine.createGlideEngine())
                .setMinSelectNum(1) //最小选择数量,默认9张
                .isDisplayCamera(isCamera) //列表是否显示拍照按钮
                .setImageSpanCount(4) //列表每行显示个数
                .isGif(isGif) //是否显示gif
                .setCompressEngine(new ImageCompressEngine())

                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        LogsUtils.i("路经==" + GsonUtils.toJson(result));
                        ArrayList<String> filePathList = new ArrayList<>();
                        for (LocalMedia localMedia : result) {
                            filePathList.add(localMedia.getCompressPath());
                        }
                        selectorListener.onResult(filePathList);
                    }

                    @Override
                    public void onCancel() {
                        selectorListener.onCancel();
                    }
                });

    }

    public ArrayList<String> getImageList() {

        ArrayList<String> resutls = new ArrayList<>();


        return resutls;
    }

    public interface onPictureSelectorListener {

        void onResult(ArrayList<String> result);

        void onCancel();
    }
}
